home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.3)
-
- import javax.comm as javax
- from serialutil import *
- VERSION = '$Revision: 1.8 $'.split()[1]
-
- def device(portnumber):
- enum = javax.comm.CommPortIdentifier.getPortIdentifiers()
- ports = []
- while enum.hasMoreElements():
- el = enum.nextElement()
- if el.getPortType() == javax.comm.CommPortIdentifier.PORT_SERIAL:
- ports.append(el)
- continue
- return ports[portnumber].getName()
-
-
- class Serial(SerialBase):
-
- def open(self):
- if self._port is None:
- raise SerialException('Port must be configured before it can be used.')
-
- if type(self._port) == type(''):
- portId = javax.comm.CommPortIdentifier.getPortIdentifier(self._port)
- else:
- portId = javax.comm.CommPortIdentifier.getPortIdentifier(device(self._port))
-
- try:
- self.sPort = portId.open('python serial module', 10)
- except Exception:
- msg = None
- self.sPort = None
- raise SerialException('Could not open port: %s' % msg)
-
- self._reconfigurePort()
- self._instream = self.sPort.getInputStream()
- self._outstream = self.sPort.getOutputStream()
- self._isOpen = True
-
-
- def _reconfigurePort(self):
- if not (self.sPort):
- raise SerialException('Can only operate on a valid port handle')
-
- self.sPort.enableReceiveTimeout(30)
- if self._bytesize == FIVEBITS:
- jdatabits = javax.comm.SerialPort.DATABITS_5
- elif self._bytesize == SIXBITS:
- jdatabits = javax.comm.SerialPort.DATABITS_6
- elif self._bytesize == SEVENBITS:
- jdatabits = javax.comm.SerialPort.DATABITS_7
- elif self._bytesize == EIGHTBITS:
- jdatabits = javax.comm.SerialPort.DATABITS_8
- else:
- raise ValueError('unsupported bytesize: %r' % self._bytesize)
- if self._stopbits == STOPBITS_ONE:
- jstopbits = javax.comm.SerialPort.STOPBITS_1
- elif stopbits == STOPBITS_ONE_HALVE:
- self._jstopbits = javax.comm.SerialPort.STOPBITS_1_5
- elif self._stopbits == STOPBITS_TWO:
- jstopbits = javax.comm.SerialPort.STOPBITS_2
- else:
- raise ValueError('unsupported number of stopbits: %r' % self._stopbits)
- if self._parity == PARITY_NONE:
- jparity = javax.comm.SerialPort.PARITY_NONE
- elif self._parity == PARITY_EVEN:
- jparity = javax.comm.SerialPort.PARITY_EVEN
- elif self._parity == PARITY_ODD:
- jparity = javax.comm.SerialPort.PARITY_ODD
- else:
- raise ValueError('unsupported parity type: %r' % self._parity)
- jflowin = jflowout = 0
- if self._rtscts:
- jflowin |= javax.comm.SerialPort.FLOWCONTROL_RTSCTS_IN
- jflowout |= javax.comm.SerialPort.FLOWCONTROL_RTSCTS_OUT
-
- if self._xonxoff:
- jflowin |= javax.comm.SerialPort.FLOWCONTROL_XONXOFF_IN
- jflowout |= javax.comm.SerialPort.FLOWCONTROL_XONXOFF_OUT
-
- self.sPort.setSerialPortParams(baudrate, jdatabits, jstopbits, jparity)
- self.sPort.setFlowControlMode(jflowin | jflowout)
- if self._timeout >= 0:
- self.sPort.enableReceiveTimeout(self._timeout * 1000)
- else:
- self.sPort.disableReceiveTimeout()
-
-
- def close(self):
- if self._isOpen:
- if self.sPort:
- self._instream.close()
- self._outstream.close()
- self.sPort.close()
- self.sPort = None
-
- self._isOpen = False
-
-
-
- def makeDeviceName(self, port):
- return device(port)
-
-
- def inWaiting(self):
- if not (self.sPort):
- raise portNotOpenError
-
- return self._instream.available()
-
-
- def read(self, size = 1):
- if not (self.sPort):
- raise portNotOpenError
-
- read = ''
- if size > 0:
- while len(read) < size:
- x = self._instream.read()
- if x == -1:
- if self.timeout >= 0:
- break
-
- self.timeout >= 0
- read = read + chr(x)
-
- return read
-
-
- def write(self, data):
- if not (self.sPort):
- raise portNotOpenError
-
- self._outstream.write(data)
-
-
- def flushInput(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self._instream.skip(self._instream.available())
-
-
- def flushOutput(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self._outstream.flush()
-
-
- def sendBreak(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.sendBreak()
-
-
- def setRTS(self, on = 1):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.setRTS(on)
-
-
- def setDTR(self, on = 1):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.setDTR(on)
-
-
- def getCTS(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.isCTS()
-
-
- def getDSR(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.isDSR()
-
-
- def getRI(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.isRI()
-
-
- def getCD(self):
- if not (self.sPort):
- raise portNotOpenError
-
- self.sPort.isCD()
-
-
- if __name__ == '__main__':
- s = Serial(0, baudrate = 19200, bytesize = EIGHTBITS, parity = PARITY_EVEN, stopbits = STOPBITS_ONE, timeout = 3, xonxoff = 0, rtscts = 0)
- s.setRTS(1)
- s.setDTR(1)
- s.flushInput()
- s.flushOutput()
- s.write('hello')
- print repr(s.read(5))
- print s.inWaiting()
- del s
-
-